package com.hmsm.server.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class WordToHtml {

	public String toHTML(String path) {
		try {
			File file = new File(path);
			if (!file.exists()) {
				return "";
			}
			WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);
			System.out.println(wordMLPackage.getContentType());
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (Docx4JException e) {
			e.printStackTrace();
		}

		return "";
	}

	private void writeFile(String path, String content) {
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.print("文件存在");
			} else {
				System.out.print("文件不存在");
				f.createNewFile();// 不存在则创建
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		WordToHtml wth = new WordToHtml();
		long time1 = System.currentTimeMillis();
		wth.writeFile("D:\\7.txt", wth.toHTML("D:\\7.docx"));
		long time2 = System.currentTimeMillis();
		System.out.println("用时：" + (time2 - time1));
	}
}
